package design.leetcode_155;

import java.util.ArrayDeque;
import java.util.Deque;

public class OnlyStackArray {
    class MinStack {
        Deque<int[]> stack;

        /**
         * initialize your data structure here.
         */
        public MinStack() {
            stack = new ArrayDeque<>();
        }

        public void push(int val) {
            if (stack.isEmpty()) {
                stack.push(new int[]{val, val});
            } else {
                stack.push(new int[]{val, Math.min(val, stack.peek()[1])});
            }
        }

        public void pop() {
            if (!stack.isEmpty()) {
                stack.pop();
            }
        }

        public int top() {
            if (!stack.isEmpty()) {
                return stack.peek()[0];
            }
            throw new RuntimeException("Error!");
        }

        public int getMin() {
            if (!stack.isEmpty()) {
                return stack.peek()[1];
            }
            throw new RuntimeException("Error!");
        }
    }
}
